package com.zhao.integration.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Messager {
	public ConnectionFactory connectionFactory() {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
		return connectionFactory;
	}

	public Connection connection(ConnectionFactory connectionFactory) throws Exception {
		Connection connection = connectionFactory.createConnection();
		return connection;
	}

	public Session session(Connection connection) throws Exception {
		connection.start();
		Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
		return session;
	}

	public MessageConsumer messageConsumer(Session session, Destination destination) throws Exception {
		MessageConsumer messageConsumer = session.createConsumer(destination);
		return messageConsumer;
	}

	public MessageProducer messageProducer(Session session, Destination destination) throws Exception {
		MessageProducer messageProducer = session.createProducer(destination);
		messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		return messageProducer;
	}

	public Destination destination(Session session, String queueName) throws Exception {
		Destination destination = session.createQueue(queueName);
		return destination;
	}

	public void close(Connection connection) throws Exception {
		if (connection != null) {
			connection.close();
		}
	}
}
